package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.UmsAdmin;
import com.zjxf.base.bean.po.UmsStore;
import com.zjxf.base.bean.ro.RoUmsStore;
import com.zjxf.base.bean.vo.VoUmsStore;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.UmsStoreService;
import com.zjxf.utils.RadomUP;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 商铺
 *
 * @author ZhangWei
 * @date 2021/3/27/027 15:56
 */
@Slf4j
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "商铺")
@RequestMapping("/umsstore/")
public class UmsStoreController extends BaseController {

    private final UmsStoreService umsStoreService;

    @ApiOperation(value = "添加或修改")
    @PostMapping(value = "add")
    public JSONResponse add(@Valid @RequestBody VoUmsStore.addOrUpdate vo) throws OperationException {
        UmsAdmin umsAdmin = (UmsAdmin) getUser();
        RadomUP.UP up= umsStoreService.add(vo, umsAdmin);
        return JSONResponse.success(up);
    }

    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public JSONResponse Update(@Valid @RequestBody VoUmsStore.addOrUpdate vo) throws OperationException {
        UmsAdmin umsAdmin = (UmsAdmin) getUser();
        umsStoreService.Update(vo, umsAdmin);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "delete")
    public JSONResponse delete(@NotNull(message = "商铺id不能为空") @RequestParam Long id) throws OperationException {
        umsStoreService.delete(id);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "分页")
    @GetMapping(value = "page")
    public JSONResponse page(@Valid @RequestBody VoUmsStore.filter filter) {
        Page<UmsStore> umsStorePage = umsStoreService.page(filter);
        JSONObject jsonData = umsStoreService.pageRo(umsStorePage);
        return JSONResponse.success(jsonData);
    }

    @ApiOperation(value = "回显")
    @GetMapping(value = "getEntity")
    public JSONResponse getEntity(@NotNull(message = "商铺id不能为空") @RequestParam Long id) throws OperationException {
        UmsStore umsStore = umsStoreService.getEntity(id);
        RoUmsStore.Details details= umsStoreService.getEntityRo(umsStore);
        return JSONResponse.success(details);
    }



}
